package com.wltt.product.ahuthr.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class RspPage<T> implements Serializable {

    /**
     * 页码
     */
    private Integer pageNo;

    /**
     * 每页数目
     */
    private Integer pageSize;

    /**
     * 结果列表
     */
    private List<T> rows;

    /**
     * 总记录数
     */
    private Long recordsTotal;

    /**
     * 总页数
     */
    private Long pageTotal;

    /**
     * 当前页记录数
     */
    private Integer rowsCount;

    public RspPage() {
    }

    /**
     * 分页数据
     *
     * @param pageNo       页码
     * @param pageSize     每页数目
     * @param rows         结果集
     * @param recordsTotal 总记录数
     * @return
     */
    public RspPage(Integer pageNo, Integer pageSize, List<T> rows, Long recordsTotal) {
        if (null == pageNo || pageNo < 1) {
            throw new IllegalArgumentException("the pageNo value was illegal.");
        }
        if (null == pageSize || pageSize < 1) {
            throw new IllegalArgumentException("the pageSize value was illegal.");
        }
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.recordsTotal = recordsTotal;
        this.rows = rows == null ? new ArrayList() : rows;
        setTotalPage();
    }


    /**
     * 设置总记录数
     */
    private void setTotalPage() {
        if (this.getRecordsTotal() == 0) {
            this.pageTotal = 0L;
        } else {
            this.pageTotal = this.getRecordsTotal() / this.getPageSize() + (this.getRecordsTotal() % this.getPageSize() == 0 ? 0 : 1);
        }
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public Integer getPageNo() {
        return pageNo;
    }

    public List<T> getRows() {
        return null == rows ? new ArrayList() : rows;
    }

    public Long getRecordsTotal() {
        if (null == recordsTotal || recordsTotal <= 0) {
            recordsTotal = 0L;
        }
        return recordsTotal;
    }

    public Long getPageTotal() {
        return pageTotal;
    }

    public Integer getRowsCount() {
        rowsCount = null == rows ? 0 : rows.size();
        return rowsCount;
    }


    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("RspPage{");
        sb.append("pageNo=").append(pageNo);
        sb.append(", pageSize=").append(pageSize);
        sb.append(", rows=").append(rows);
        sb.append(", recordsTotal=").append(recordsTotal);
        sb.append(", pageTotal=").append(pageTotal);
        sb.append(", rowsCount=").append(rowsCount);
        sb.append('}');
        return sb.toString();
    }
}
